<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_match
{

    //Methods
    public function __construct()
    {

    }

    public function get_match_by_game_id_and_parent_id($game_id, $parent_id) {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, i.name AS industry_name, c.name AS win_choice_name " 
                . "FROM tbl_match m LEFT JOIN tbl_industry i ON m.industry_id = i.id " 
                . "LEFT JOIN tbl_choice c ON c.id = m.win_choice_id " 
                . "WHERE m.parent_id = '$parent_id' "
                . "AND m.game_id = '$game_id' ";
        //        echo $query;
        //        exit();
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();

        return $row;
    }
    
    public function update_roulette_win_choice_id($parent_id, $win_choice_id, $game_id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_match SET win_choice_id = $win_choice_id "
                . "WHERE parent_id = $parent_id "
                . "AND game_id = $game_id ";
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $db->close_connect();

        return TRUE;
    }
    
    public function update_win_choice_id_by_id($id, $win_choice_id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_match SET win_choice_id = $win_choice_id "
                . "WHERE id = $id ";
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $db->close_connect();

        return TRUE;
    }
    
    public function get_all()
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, i.name AS industry_name " . "FROM tbl_match m LEFT JOIN tbl_industry i ON m.industry_id = i.id " . "ORDER BY m.date_end DESC ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function get_all_bet_matches()
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, i.name AS industry_name " . "FROM tbl_match m LEFT JOIN tbl_industry i ON m.industry_id = i.id " . "ORDER BY m.date_end DESC " . "WHERE m.type_id = 1 ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function get_all_roulettes()
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, i.name AS industry_name " 
                . "FROM tbl_match m LEFT JOIN tbl_industry i ON m.industry_id = i.id " 
                . "WHERE m.type_id = 2 "
                . "AND m.game_id = 0 " 
                . "ORDER BY m.date_end DESC ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function get_all_normal()
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, i.name AS industry_name " 
                . "FROM tbl_match m LEFT JOIN tbl_industry i ON m.industry_id = i.id " 
                . "WHERE m.type_id = 1 "
                . "ORDER BY m.date_end DESC ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }
    
    public function get_match_live_by_id($id, $date)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, i.name AS industry_name " . "FROM tbl_match m LEFT JOIN tbl_industry i ON m.industry_id = i.id " . "WHERE m.id = " . $id . " " . "AND date_start <= '" . $date . ":59' " . "AND date_end >= '" . $date . ":00' ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();

        return $row;
    }

    public function get_all_hottest()
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT *, SUBTIME(date_end, date_start) AS date_remaining " . "FROM tbl_match " . "WHERE is_hottest = 1 " . "ORDER BY date_start DESC ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function get_all_live_in($date)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT *, SUBTIME(date_end, date_start) AS date_remaining " . "FROM tbl_match " 
                . "WHERE type_id = 1 AND date_start <= '" . $date . ":59' " . "AND date_end >= '" . $date . ":00' " . "ORDER BY date_start DESC ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function cancel($id, $reason_cancel)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "UPDATE tbl_match SET " . "is_cancel = 1," . "cancel_reason = '" . $reason_cancel . "' " . "WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $db->close_connect();

        return TRUE;
    }

    public function save($id, $name, $description, $date_start, $date_end, $country_code, $country_name, $industry_id, $commission, $profit, $win_choice_id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        if ($id == 0) {
            $query = "INSERT INTO tbl_match(name, description, date_start, date_end, country_code, country_name, industry_id, commission, profit, win_choice_id) VALUES (
                '" . $name . "','" . $description . "','" . $date_start . "','" . $date_end . "','" . $country_code . "','" . $country_name . "'," . $industry_id . "," . $commission . "," . $profit . "," . $win_choice_id . ")";
        } else {
            $query = "UPDATE tbl_match SET " . "name = '" . $name . "'," . "description = '" . $description . "'," . "date_start = '" . $date_start . "'," . "date_end = '" . $date_end . "'," . "country_code = '" . $country_code . "'," . "country_name = '" . $country_name . "'," . "industry_id = " . $industry_id . "," . "commission = " . $commission . "," . "profit = " . $profit . "," . "win_choice_id = " . $win_choice_id . " " . "WHERE id = " . $id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $inserted_id = mysqli_insert_id($db->con);
        $db->close_connect();

        return $inserted_id;
    }

    public function get_by_id($id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, i.name AS industry_name, c.name AS win_choice_name " 
                . "FROM tbl_match m LEFT JOIN tbl_industry i ON m.industry_id = i.id " 
                . "LEFT JOIN tbl_choice c ON c.id = m.win_choice_id " 
                . "WHERE m.id = " . $id;
        //        echo $query;
        //        exit();
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();

        return $row;
    }

    public function delete($id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $children = $this->get_game_list_by_roulette($id);
        foreach($children as $child){
            $query_child = "DELETE FROM tbl_match WHERE id = " . $child['id'];
            mysqli_query($con, $query_child) or die("Query fail: " . mysqli_error());
        }

        $query = "DELETE FROM tbl_match WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();

        return TRUE;
    }

    public function get_game_list_by_roulette($id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_match WHERE parent_id = $id";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function save_roulette($id, $name, $date_start, $date_end)
    {
        $db  = new connection();
        $con = $db->open_connect();

        if ($id == 0) {
            $query = "INSERT INTO tbl_match(name, date_start, date_end, type_id) VALUES ('$name', '$date_start', '$date_end', 2)";
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
            $p_id = mysqli_insert_id($con);
            for ($i = 0; $i < 6; $i++) {
                $game_id = $i + 1;
                $dao_roulette_game = new dao_roulette_game();
                $game = $dao_roulette_game->find($game_id);
                $game_name = $game['name'];
                $query_child = "INSERT INTO tbl_match(name, date_start, date_end, type_id, parent_id, game_id) VALUES ('$name - $game_name', '$date_start', '$date_end', 2, $p_id, $game_id)";
                mysqli_query($con, $query_child) or die("Query fail: " . mysqli_error());
            }
        } else {
            $query = "UPDATE tbl_match SET name = '$name', date_start = '$date_start', date_end = '$date_end' WHERE id = $id";
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
            $children = $this->get_game_list_by_roulette($id);
            foreach ($children as $child) {
                $game_id = $child['game_id'];
                $dao_roulette_game = new dao_roulette_game();
                $game = $dao_roulette_game->find($game_id);
                $game_name = $game['name'];
                
                $query_child = "UPDATE tbl_match SET name = '$name - $game_name', date_start = '$date_start', date_end = '$date_end' WHERE id = " . $child['id'];
                mysqli_query($con, $query_child) or die("Query fail: " . mysqli_error());
            }
        }
        $db->close_connect();

        return TRUE;
    }

    public function get_roulette_by_id($id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT m.*, c.name AS win_choice_name "
                . "FROM tbl_match m "
                . "LEFT JOIN tbl_choice c ON c.id = m.win_choice_id "
                . "WHERE m.id = $id";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();

        return $row;
    }
}
